package com.example.jf.jfapp.activity.base;

/**
 * Created by jf on 2017/7/26.
 */

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.example.jf.jfapp.R;
import com.example.jf.jfapp.utils.ActivityManageUtil;

/**
 * 最基础基类
 * 提供代码规范方法
 * initView()
 * getData()
 * 子类重写即可，也可以用抽象方法
 */
public abstract class BaseActivity extends AppCompatActivity {
    public static String TAG;
    ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManageUtil.getInstance().addActivity(this);
        TAG = getClass().getName();
        getData();
    }

    /**
     * 调用此方法必须保证布局下有个id为toolbar的View
     */
    public void setToolbar(String title) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setToolbar(title,toolbar);
    }

    //搜索类名 Ctrl + shift + N

    public void setToolbar(String title,Toolbar toolbar){
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle(title);
        }
    }



    /**
     * 初始化View，可以获取View数据设置Adapter等
     */
    public void initView() {}

    /**
     * 获取数据，可以在这里对网络进行访问
     */
    public void getData() {}

    public void showProgressDialog(String msg) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setCancelable(false);

        }
        mProgressDialog.setMessage("" + msg);
        mProgressDialog.show();
    }

    public void dismissProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

    public void showToast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BaseActivity.this, msg + "", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManageUtil.getInstance().finishActivity(this);
    }
}

